package com.slunk.physics.characters;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.math.MathUtils;
import com.slunk.utilities.EntityCategories;
import com.slunk.utilities.Movement;
import java.util.Timer;
import java.util.TimerTask;


public class AI_Person extends Character {
	//used for the random variables in speed in schedule times
	private static float MIN_SPEED = 0.0f;
	private static float MAX_SPEED = 0.2f;
	private static int MIN_SCHEDULE = 1000;
	private static int MAX_SCHEDULE = 5000;
	
	public Timer timer;
	private int schedule;
	
	public AI_Person(World world, Vector2 position, float boxWidth, float boxHeight) {
		super(world, position, boxWidth, boxHeight, EntityCategories.PERSON.getEntityValue(), (short)(EntityCategories.BOUNDARY.getEntityValue() | EntityCategories.CAR.getEntityValue() | EntityCategories.TRAIN.getEntityValue()));
		
		randomMovement();
		this.speed = MathUtils.random(MIN_SPEED,MAX_SPEED);
		this.schedule = MathUtils.random(MIN_SCHEDULE,MAX_SCHEDULE);
		timer = new Timer();
        timer.schedule(new AIPersonTimerTask(), this.schedule);
        
        //this will just override the user data for Character
        this.body.setUserData(this);
        
	}
	
	private void randomMovement() {
		int rand = MathUtils.random(0, 9);
		
		switch(rand) {
		case 0: {
			setMovementState(Movement.STILL);
			break;
		}
		case 1: {
			setMovementState(Movement.NORTH);
			break;
		}
		case 2: {
			setMovementState(Movement.NORTHEAST);
			break;
		}
		case 3: {
			setMovementState(Movement.EAST);
			break;
		}
		case 4: {
			setMovementState(Movement.SOUTHEAST);
			break;
		}
		case 5: {
			setMovementState(Movement.SOUTH);
			break;
		}
		case 6: {
			setMovementState(Movement.SOUTHWEST);
			break;
		}
		case 7: {
			setMovementState(Movement.WEST);
			break;
		}
		case 8: {
			setMovementState(Movement.NORTHWEST);
			break;
		}
		}
	}
	
	public void dispose() {
		this.timer.cancel();
	}
	
	//Timer class to randomly keep changing the movement
	class AIPersonTimerTask extends TimerTask {
        public void run() {
        	randomMovement();
        	speed = MathUtils.random(MIN_SPEED,MAX_SPEED);
    		schedule = MathUtils.random(MIN_SCHEDULE,MAX_SCHEDULE);
    		
    		timer.schedule(new AIPersonTimerTask(), schedule);
        }
    }
}
